<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>Insert_Title_Here</title>
		<link rel="Shortcut Icon" href="public/img/favicon.ico" />
		<link rel="stylesheet" href="public/bootstrap/css/bootstrap.min.css" />
		<script src="public/js/jquery-3.2.1.min.js"></script>
		<script src="public/bootstrap/js/bootstrap.min.js"></script>
		<link rel="stylesheet" href="public/css/style.css" />
		<script src="public/js/holder.min.js"></script>
	</head>
	<style>
		th, td{
			width:300px;
			text-align: center;
		}
	</style>
	<script type="text/javascript">
		 function jsCopy(ev){
	        var ele=ev.parentNode;
	        var elem_child = ele.childNodes;
	        for(var i=0 in elem_child){ //遍历子元素数组
	            if(elem_child[i].nodeName == "#text" && !/\S/.test(elem_child[i].nodeValue)) {
	                //删除数组中的text
	                ele.removeChild(elem_child[i]);
	            }
	        }
	    }
		function approve(fileId, upId, obj){
			//去除行的text元素
			jsCopy(obj.parentNode);
			var cell = obj.parentNode.parentNode.childNodes[3]; //找到cell
			var select = cell.firstElementChild;
			$.ajax({
				url:"${pageContext.request.contextPath}/adminAsy?method=approve",
				async:true,
				type:"POST",
				data:{"fileId":fileId,"upId":upId,"fileType":select.value},
				success:function(WebData){
					if(WebData.isSuc){
						alert("审批成功！");
						select.parentNode.parentNode.remove();
					}else
						alert("审批失败！");
				},
				error:function(){
					alert("请求失败");
				},
				dataType:"json"
			});
		}
		function notApprove(fileId, upId, obj){
			jsCopy(obj.parentNode);
			var cell = obj.parentNode.parentNode.childNodes[3]; //找到cell
			var select = cell.firstElementChild;
			$.ajax({
				url:"${pageContext.request.contextPath}/adminAsy?method=notApprove",
				async:true,
				type:"POST",
				data:{"fileId":fileId,"upId":upId},
				success:function(WebData){
					if(WebData.isSuc){
						alert("未审批成功");
						select.parentNode.parentNode.remove();
					}
				},
				error:function(){
					alert("请求失败");
				},
				dataType:"json"
			});
		}
		function downloadFile(fileId, upUser){
	        var url = "${pageContext.request.contextPath}/homeAsy?method=download";
	        var fileName = upUser+fileId;
	        var form = $("<form></form>").attr("action", url).attr("method", "post");
	        form.append($("<input></input>").attr("type", "hidden").attr("name", "fileId").attr("value", fileName));
	        form.appendTo('body').submit().remove();
		}
		
		function deleteFile(fileId, upUser, obj){
			$.ajax({
				url:"${pageContext.request.contextPath}/adminAsy?method=deleteFile",
				async:true,
				type:"POST",
				data:{"fileId":fileId,"upId":upUser},
				success:function(WebData){
					if(WebData.isSuc){
						alert("刪除成功");
						obj.parentNode.parentNode.remove();
					}
				},
				error:function(){
					alert("请求失败");
				},
				dataType:"json"
			});
		}
		function frozenFile(fileId, upUser, obj){
			var status = null;
			var str = obj.innerHTML.replace(/(^\s*)|(\s*$)/g, "");
			if(str == "被冻结"){
				status = "1";
			}else if(str == "可下载"){
				status = "2";
			}
			$.ajax({
				url:"${pageContext.request.contextPath}/adminAsy?method=frozenFile",
				async:true,
				type:"POST",
				data:{"fileId":fileId,"upId":upUser,"status":status},
				success:function(WebData){
					if(WebData.isSuc){
						alert("操作成功");
						if(str == "被冻结"){
							obj.innerHTML="可下载";
						}else if(str == "可下载"){
							obj.innerHTML="被冻结";
						}
					}
				},
				error:function(){
					alert("请求失败");
				},
				dataType:"json"
			});
		}
		function frozenUser(userId, obj){
			var str = obj.innerHTML.replace(/(^\s*)|(\s*$)/g, "");
			var status = null;
			if(str == "正常"){
				status = "2";
			}else if(str == "冻结"){
				status = "1";
			}
			$.ajax({
				url:"${pageContext.request.contextPath}/adminAsy?method=userFrozen",
				async:true,
				type:"POST",
				data:{"userId":userId, "status":status},
				success:function(WebData){
					if(WebData.isSuc){
						alert("操作成功");
						if(str == "正常"){
							obj.innerHTML="冻结";
						}else if(str == "冻结"){
							obj.innerHTML="正常";
						}
					}
				},
				error:function(){
					alert("请求失败");
				},
				dataType:"json"
			});
		}
		function postNotice(){
			var str = document.getElementById("notice").value;
			if(str.length > 40)
			{
				alert("字数太长！");
				return;
			}
			$.ajax({
				url:"${pageContext.request.contextPath}/adminAsy?method=postNotice",
				async:true,
				type:"POST",
				data:{"msg":str},
				success:function(WebData){
					if(WebData.isSuc){
						alert("发布成功");
					}
				},
				error:function(){
					alert("发布失败");
				},
				dataType:"json"
			});
		}
		upUserCft = null;
		fileIdCft = null;
		objCft = null
		function changeFileType1(upUser, fileId, obj){
			upUserCft = upUser;
			fileIdCft = fileId;
			objCft = obj;
		}
		function changeFileType(obj){
			jsCopy(obj.parentNode);
			fileType = obj.parentNode.parentNode.childNodes[1].firstElementChild.value;
			$.ajax({
				url:"${pageContext.request.contextPath}/adminAsy?method=changeFileType",
				async:true,
				type:"POST",
				data:{"fileId":upUserCft+fileIdCft, "fileType":fileType},
				success:function(WebData){
					if(WebData.isSuc){
						alert("修改成功");
						objCft.firstElementChild.innerHTML=fileType;
					}
				},
				error:function(){
					alert("修改失败");
				},
				dataType:"json"
			});
		}
	</script>
	<body>
		<!--顶部效果-->
		<%@ include file= "/WEB-INF/zpublicpages/header.jsp" %>
		
		<!--主体部分-->
		<div class="container" style="margin-top: 10px; font-size: 20px;">
			<div class="row">
				<ul id="myTab" class="nav nav-tabs">
					<li class="active">
						<a href="#fileAppro" data-toggle="tab">文件审批</a>
					</li>
					<li>
						<a href="#fileSort" data-toggle="tab">文件整理</a>
					</li>
					<li>
						<a href="#AccFrozen" data-toggle="tab">账户冻结</a>
					</li>
					<li>
						<a href="#attention" data-toggle="tab">公告编辑</a>
					</li>
				</ul>
				<div id="myTabContent" class="tab-content" style="margin-top: 20px;line-height: 2;">
					<div class="tab-pane fade in active" id="fileAppro">
						<table style="margin: 0 auto;">
							<tr>
								<th style="width:40%;">文件名</th>
								<th>上传者</th>
								<th>大小</th>
								<th>类别</th>
								<th>通过</th>
								<th>不通过</th>
							</tr>
							<c:forEach items = "${requestScope.notAppro}" var = "eNotAppro">
								<tr>
									<td>
										<button onclick="downloadFile('${eNotAppro.fileId}', '${eNotAppro.userId}')"
											 style="border:0px;background-color:transparent;">
											${eNotAppro.fileId}
										</button>
									</td>
									<td>${eNotAppro.userId}</td>
									<td>${eNotAppro.size}&nbsp;kb</td>
									<td>
										<select style="width:130px;text-align:center;">
											<option value="Learn">&nbsp;学习资源</option>
											<option value="Play">&nbsp;影音娱乐</option>
											<option value="IDE">&nbsp;开发工具</option>
											<option value="OfficeFile">&nbsp;政策文件</option>
										</select>
									</td>
									<td>
										<button onclick = "approve('${eNotAppro.fileId}', '${eNotAppro.userId}', this)" 
											 style="border:0px;background-color:transparent;">
											<div class="glyphicon glyphicon-ok"></div>
										</button>
									</td>
									<td>
										<button onclick = "notApprove('${eNotAppro.fileId}', '${eNotAppro.userId}', this)" 
											 style="border:0px;background-color:transparent;">
											<div class="glyphicon glyphicon-remove"></div>
										</button>
									</td>
								</tr>
							</c:forEach>
						</table>
					</div>
					<div class="tab-pane fade" id="fileSort">
						<table style="margin: 0 auto;">
							<tr>
								<th style="width:40%;">文件名</th>
								<th>上传者</th>
								<th>类别</th>
								<th>删除</th>
								<th>文件状态</th>
							</tr>
							<c:forEach items = "${requestScope.fileArr}" var = "eFile">
								<tr>
									<td>
										<button onclick="downloadFile('${eFile.fileId}', '${eFile.upUser}')"
											 style="border:0px;background-color:transparent;">
											${eFile.fileId}
										</button>
									</td>
									<td>${eFile.upUser}</td>
									<td>
										<button class="btn" style="background:white;" onclick="changeFileType1('${eFile.upUser}','${eFile.fileId}',this)" data-toggle="modal" data-target="#myModal">
											<span style="font-size: 20px;">${eFile.fileType}</span>
										</button>
									</td>
									<td>
										<button onclick = "deleteFile('${eFile.fileId}', '${eFile.upUser}', this)"
											 style="border:0px;background-color:transparent;">
											<div class="glyphicon glyphicon-remove"></div>
										</button>
									</td>
									<td>
										<button onclick = "frozenFile('${eFile.fileId}', '${eFile.upUser}', this)" 
											 style="border:0px;background-color:transparent;">
											<c:if test="${eFile.status eq 1}">
												可下载
											</c:if>
											<c:if test="${eFile.status eq 2}">
												被冻结
											</c:if>
										</button>
									</td>
								</tr>
							</c:forEach>
						</table>
					</div>
					<div class="tab-pane fade" id="AccFrozen">
						<table style="margin: 0 auto;">
							<tr>
								<th>用户id</th>
								<th>用户名</th>
								<th>用户状态</th>
							</tr>
							<c:forEach items = "${requestScope.users}" var = "euser">
								<tr>
									<td>${euser.userId}</td>
									<td>${euser.userName}</td>
									<td>
										<button onclick = "frozenUser('${euser.userId}', this)" 
											 style="border:0px;background-color:transparent;">
											<c:if test="${euser.status eq 1}">
												正常
											</c:if>
											<c:if test="${euser.status eq 2}">
												冻结
											</c:if>
										</button>
									</td>
								</tr>
							</c:forEach>
						</table>
						
					</div>
					<div class="tab-pane fade" id="attention" style="margin-bottom:20%;">
						<div style="margin-bottom:10px; margin-left:20px;">公告编辑：</div>
					
						<form action="#" class="form-horizontal" onsubmit="return false">
						 	<div class="form-group" style="width:90%; margin:0 auto;">
						   		<textarea id="notice" class="form-control" rows="3" style="font-size:20px;"></textarea>
						  	</div>
							<div class="form-group">
							   <div class="col-sm-offset-2 col-sm-10" style="float:right;pading-right:20px;">
								   <button type="submit" class="btn btn-primary btn-lg" onclick="postNotice()">发布公告</button>
							   </div>
							</div>
						</form>
					</div>
					<div class="tab-pane fade" id="usersMsgs">
						
					</div>
				</div>
			</div>
		</div>
		<!-- 模态框（Modal） -->
		<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
		    <div class="modal-dialog">
		        <div class="modal-content">
		            <div class="modal-header">
		                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
		                <h4 class="modal-title" id="myModalLabel">修改文件类型</h4>
		            </div>
		            <div class="modal-body">
		            	<select style="width:130px;text-align:center;font-size:20px;margin-left:38%;">
							<option value="Learn">&nbsp;学习资源</option>
							<option value="Play">&nbsp;影音娱乐</option>
							<option value="IDE">&nbsp;开发工具</option>
							<option value="OfficeFile">&nbsp;政策文件</option>
						</select>
		            </div>
		            <div class="modal-footer">
		                <button type="button" onclick="changeFileType(this);" class="btn btn-default" data-dismiss="modal">OK</button>
		            </div>
		        </div><!-- /.modal-content -->
		    </div><!-- /.modal -->
		</div>
				
				
		<!--底部效果-->
		<%@ include file="/WEB-INF/zpublicpages/footer.jsp"%>
		
	</body>
</html>